package studio.raptor.hub.in.task;


import com.jayway.jsonpath.JsonPath;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.hub.common.dto.EventMeta;
import studio.raptor.hub.common.model.Pair;
import studio.raptor.hub.in.constant.EventKindConstant;
import studio.raptor.hub.in.constant.JsonElementName;
import studio.raptor.hub.in.entity.ServerProxyReq;
import studio.raptor.hub.in.server.AccountProxy;
import studio.raptor.hub.in.server.CustomerProxy;
import studio.raptor.hub.in.server.OfferInstProxy;
import studio.raptor.hub.in.server.ProdInstProxy;
import studio.raptor.hub.in.server.SoCompleteARPProxy;
import studio.raptor.hub.in.server.SoExecutionProxy;
import studio.raptor.hub.in.server.SoOrderCommittedProxy;
import studio.raptor.hub.in.server.SoOrderCompleteProxy;

/**
 * Created by liuyong8 on 2017/9/19.
 */
public class EventNodeTask implements Callable<Pair<String, String>> {
  private static final Logger log = LoggerFactory.getLogger(EventNodeTask.class);
  private HttpClient httpClient;
  private Integer kindId;
  private String jsonNodeName;
  private String serverUrl;
  private String serverType;
  private EventMeta eventMeta;

  public EventNodeTask(Integer kindId, String jsonNodeName, String serverUrl,
      String serverType, EventMeta eventMeta, HttpClient httpClient) {
    this.kindId = kindId;
    this.jsonNodeName = jsonNodeName;
    this.serverUrl = serverUrl;
    this.serverType = serverType;
    this.eventMeta = eventMeta;
    this.httpClient = httpClient;
  }

  @Override
  public Pair<String, String> call() throws Exception {
    StringBuilder sb = new StringBuilder();
    List<Long> params = new ArrayList<>();
    ServerProxyReq serverProxyReq = new ServerProxyReq();
    serverProxyReq.setUrl(serverUrl);
    serverProxyReq.setServerType(serverType);

    try {
      if (JsonElementName.CUSTOMER_ORDER.equals(jsonNodeName) || JsonElementName.CUSTOMER_ORDERS
          .equals(jsonNodeName)) {
        if (EventKindConstant.SO_ORDER_COMMITTED.equals(kindId)) {
          SoOrderCommittedProxy soOrderCommittedProxy = new SoOrderCommittedProxy.Builder()
              .httpClient(httpClient).build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[]{}));
          sb.append(soOrderCommittedProxy.queryServerMsg(serverProxyReq)).append(",");
        } else if (EventKindConstant.SO_EXECUTION.equals(kindId)) {
          SoExecutionProxy soExecutionProxy = new SoExecutionProxy.Builder().httpClient(httpClient)
              .build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[]{}));
          sb.append(soExecutionProxy.queryServerMsg(serverProxyReq)).append(",");
        //} else if (EventKindConstant.SO_COMPLETE_ORDERITEM.equals(kindId)) {
          // 由蒿亭那边处理，不走databus
        } else if (EventKindConstant.SO_COMPLETE_ARCHIVEGROUP.equals(kindId)) {
          SoCompleteARPProxy soCompleteARPProxy = new SoCompleteARPProxy.Builder()
              .httpClient(httpClient).build();
          String content = eventMeta.getContent();
          List<Long> orderItemIds = JsonPath.parse(content).read("$.orderItemIds");
          params.addAll(orderItemIds);
          serverProxyReq.setParams(params.toArray(new Long[]{}));
          sb.append(soCompleteARPProxy.queryServerMsg(serverProxyReq)).append(",");
        } else if (EventKindConstant.SO_ORDER_COMPLETE.equals(kindId)) {
          SoOrderCompleteProxy soOrderCompleteProxy = new SoOrderCompleteProxy.Builder()
              .httpClient(httpClient).build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[]{}));
          sb.append(soOrderCompleteProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      } else if (JsonElementName.CUSTOMER_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getCustId() != null) {
          for (Long custId : eventMeta.getCustId()) {
            CustomerProxy customerProxy = new CustomerProxy.Builder().httpClient(httpClient)
                .build();
            params.add(custId);
            serverProxyReq.setParams(params.toArray(new Long[]{}));
            sb.append(customerProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        } else {
          sb.append(",");
        }
      } else if (JsonElementName.ACCOUNT_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getAcctId() != null) {
          for (Long acctId : eventMeta.getAcctId()) {
            AccountProxy accountProxy = new AccountProxy.Builder().httpClient(httpClient).build();
            params.add(acctId);
            serverProxyReq.setParams(params.toArray(new Long[]{}));
            sb.append(accountProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        } else {
          sb.append(",");
        }
      } else if (JsonElementName.OFFER_INST_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getOfferInstId() != null) {
          for (Long offerId : eventMeta.getOfferInstId()) {
            OfferInstProxy offerInstProxy = new OfferInstProxy.Builder().httpClient(httpClient)
                .build();
            params.add(offerId);
            params.add(eventMeta.getRegionId());
            serverProxyReq.setParams(params.toArray(new Long[]{}));
            sb.append(offerInstProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        } else {
          sb.append(",");
        }
      } else if (JsonElementName.OFFER_PROD_INST_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getProdInstId() != null) {
          for (Long prodId : eventMeta.getProdInstId()) {
            ProdInstProxy prodInstProxy = new ProdInstProxy.Builder().httpClient(httpClient)
                .build();
            params.add(prodId);
            params.add(eventMeta.getRegionId());
            serverProxyReq.setParams(params.toArray(new Long[]{}));
            sb.append(prodInstProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        } else {
          sb.append(",");
        }

      } else if (JsonElementName.EVENT_MSG.equals(jsonNodeName)) {
        sb.append(eventMeta.getContent()).append(",");
      }
    } catch (Exception e) {
      log.info("query event node {} failed", jsonNodeName, e);
    }

    Pair<String, String> pair = new Pair<String, String>(jsonNodeName,
        sb.toString().substring(0, sb.toString().length() - 1));

    log.debug("pair = {}",pair);

    return pair;
  }
}
